package jmine.tec.scheduling.web.pages.task;

import java.util.ArrayList;
import java.util.List;

import jmine.tec.scheduling.domain.ScheduledTask;
import jmine.tec.web.wicket.cronExpression.CronExpressionComponent;
import jmine.tec.web.wicket.cronExpression.CronExpressionTypeEnum;
import jmine.tec.web.wicket.pages.form.FormPage;
import jmine.tec.web.wicket.pages.form.FormType;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.model.PropertyModel;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.UrlPermission;

/**
 * Edição das tarefas agendadas
 */
@Secure(id = "URL_EDIT_SCHEDULED_TASK", permissionType = UrlPermission.class)
public class EditScheduledTaskPage extends FormPage<ScheduledTask> {

    private String cron;

    /**
     * Construtor.
     * 
     * @param source source page
     * @param sourcePageParameters {@link PageParameters}
     * @param entity entity
     * @param formType {@link FormType}
     */
    public EditScheduledTaskPage(Class<? extends Page> source, PageParameters sourcePageParameters, ScheduledTask entity, FormType formType) {
        super(source, sourcePageParameters, entity, formType);
    }

    /**
     * Construtor.
     * 
     * @param source source page
     * @param sourcePageParameters {@link PageParameters}
     */
    public EditScheduledTaskPage(Class<? extends Page> source, PageParameters sourcePageParameters) {
        super(source, sourcePageParameters);
    }

    /**
     * Construtor.
     * 
     * @param pageParameters {@link PageParameters}
     */
    public EditScheduledTaskPage(PageParameters pageParameters) {
        super(pageParameters);
    }

    // private List<String> listActions() {
    // List<String> result = new ArrayList<String>();
    //
    // result.add("noOperationAction");
    //
    // return result;
    // }
    //
    // private List<String> listConditions() {
    // List<String> result = new ArrayList<String>();
    // result.add("alwaysExecuteCondition");
    // result.add("onlyMasterNodeCondition");
    // return result;
    // }

    /**
     * Booleans
     * 
     * @return options
     */
    private List<Boolean> listBoolean() {
        List<Boolean> result = new ArrayList<Boolean>();

        result.add(true);
        result.add(false);
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<Component> createFormComponents() {
        List<Component> components = new ArrayList<Component>();

        components.add(this.createTextField("mnemonic"));
        components.add(this.createTextField("description"));
        components.add(this.createDropDown("active", this.listBoolean()));
        // components.add(this.createTextField("cron"));
        // components.add(this.createDropDown("condition",
        // this.listConditions()));
        // components.add(this.createDropDown("action", this.listActions()));

        if ((this.getEntity() != null) && (this.getEntity().getCron() != null)) {
            this.cron = this.getEntity().getCron();
            // identificando o tipo da cron expression (fixa ou periódica)
            CronExpressionTypeEnum cronExpressionType = CronExpressionTypeEnum.FIXED_SCHEDULE;
            if (this.cron.contains("/")) {
                cronExpressionType = CronExpressionTypeEnum.PERIODIC;
            }

            CronExpressionComponent cronHorarioFixoCompleto =
                    new CronExpressionComponent("cron", cronExpressionType.createStrategy(), new PropertyModel<String>(this, "cron"));
            cronHorarioFixoCompleto.setCronExpression(this.cron);
            components.add(cronHorarioFixoCompleto);
        }
        return components;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean beforeSave(ScheduledTask target) {
        target.setCron(this.cron);

        return super.beforeSave(target);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        return null;
    }

    /**
     * @return the cron
     */
    public String getCron() {
        return this.cron;
    }

    /**
     * @param cron the cron to set
     */
    public void setCron(String cron) {
        this.cron = cron;
    }

}